package wechat;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.weixin4j.Configuration;
import org.weixin4j.Menu;
import org.weixin4j.Weixin;
import org.weixin4j.WeixinException;
import org.weixin4j.spi.HandlerFactory;
import org.weixin4j.spi.IMessageHandler;
import org.weixin4j.ticket.TicketType;
import org.weixin4j.util.TokenUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.CommonUtil;
import com.filter.BaseController;

import beans.user.user;
import service.SpringContextUtil;
import service.user.WXUserServiceImpl;

@Controller
@RequestMapping("/wxcall")
public class WXCallController extends BaseController{

	@RequestMapping("/test")
	@ResponseBody
	public void Test() throws Exception {
		Weixin wx = new Weixin();
		wx.login(Configuration.getOAuthAppId(), Configuration.getOAuthSecret());//登录
		String url = wx.createQrcode(TicketType.QR_SCENE,"F:\\aa.jpg","1",3600);
		try {
			getResponse().getWriter().write(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/createmenu")
	@ResponseBody
	public void WeixinCreateMenu() throws WeixinException {
		try {
			JSONArray array = new JSONArray();
			JSONObject button1 = new JSONObject();
			button1.put("type", "view");
			button1.put("name", "免费试用");
			button1.put("url", "http://2.zydkj.net.cn/wxsubject/free");
			array.add(button1);
			
			JSONArray tem = new JSONArray();
			JSONObject button21 = new JSONObject();
			button21.put("type", "view");
			button21.put("name", "一把过驾考速成");
			button21.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Configuration.getOAuthAppId()+"&redirect_uri=http%3a%2f%2f2.zydkj.net.cn%2fwx%2findex&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");
			tem.add(button21);
			
			JSONObject button22 = new JSONObject();
			button22.put("type", "view");
			button22.put("name", "使用说明");
			button22.put("url", "http://2.zydkj.net.cn/tg/homeindex");
			tem.add(button22);

			JSONObject button2 = new JSONObject();
			button2.put("name", "科一科四");
			button2.put("sub_button", tem);
			array.add(button2);
			
			tem = new JSONArray();
			JSONObject button31 = new JSONObject();
			button31.put("type", "view");
			button31.put("name", "开通系统");
			button31.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Configuration.getOAuthAppId()+"&redirect_uri=http%3a%2f%2f2.zydkj.net.cn%2fwxpay%2fpayindex&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");
			tem.add(button31);
			JSONObject button33 = new JSONObject();
			button33.put("type", "view");
			button33.put("name", "绑定序号");
			button33.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+Configuration.getOAuthAppId()+"&redirect_uri=http%3a%2f%2f2.zydkj.net.cn%2fwx%2fbindxh&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect");
			tem.add(button33);
			JSONObject button32 = new JSONObject();
			button32.put("type", "view");
			button32.put("name", "软件代理");
			button32.put("url", "http://2.zydkj.net.cn/tg/jm");
			tem.add(button32);
			
			JSONObject button3 = new JSONObject();
			button3.put("sub_button", tem);
			button3.put("name", "用户中心");
			array.add(button3);

			JSONObject json = new JSONObject();
			json.put("button", array);

			JSONObject jsonall = new JSONObject();
			jsonall.put("menu", json);

			Menu menu = new Menu(jsonall);
			System.out.println("登陆22");
			Weixin wx = new Weixin();
			wx.login(Configuration.getOAuthAppId(), Configuration.getOAuthSecret());//登录
			wx.createMenu(menu);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		try {
			getResponse().getWriter().write("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/weixin")
	@ResponseBody
	public void WeixinUrlSet() throws Exception{
		boolean isGet = getReq().getMethod().toLowerCase().equals("get");
		if (isGet ) {
			String signature = getReq().getParameter("signature"); // 微信加密签名
			String timestamp = getReq().getParameter("timestamp"); // 时间戳
			String nonce = getReq().getParameter( "nonce");// 随机数
			String echostr = getReq().getParameter( "echostr");// 随机字符串
			String token = TokenUtil.get();

			if(CommonUtil.isNullObject(signature)||CommonUtil.isNullObject(timestamp)||CommonUtil.isNullObject(nonce)) {
				getResponse().getWriter().write("");
				return;
			}

			if (TokenUtil.checkSignature(token, signature, timestamp, nonce))
				getResponse().getWriter().write(echostr);
			else 
				getResponse().getWriter().write("");
		} else {
			try {
				getResponse().setCharacterEncoding("UTF-8");
				getResponse().setContentType("text/xml");
				//获取POST流
				ServletInputStream in = getReq().getInputStream();
				if (Configuration.isDebug()) {
					System.out.println("接收到微信输入流,准备处理...");
				}
				IMessageHandler messageHandler = HandlerFactory.getMessageHandler();
				//处理输入消息，返回结果
				String xml = messageHandler.invoke(in);
				getResponse().getWriter().write(xml);
			} catch (IOException e ) {
				e.printStackTrace();
			}
		}
	}

}
